﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace MixNP.Web.Classifieds.UserControls
{
    public partial class HorizontalItemCardControl : System.Web.UI.UserControl
    {
        public string _ItemId;
        public string ItemId
        {
            get
            {
                return this._ItemId;
            }
            set
            {
                this._ItemId = value;
                ShowItem();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Pes.Utility.CryptoRandom r = new Pes.Utility.CryptoRandom();

            card.ID = "card" + r.Next(1, 599999999).ToString();
        }

        private void ShowItem()
        {
            double itemId = Pes.Utility.Conversion.TryCastDouble(this._ItemId);

            using (System.Data.DataTable table = MixNP.BusinessLayer.Items.GetItem(itemId))
            {
                if (table.Rows.Count > 0)
                {
                    System.Data.DataRow row = table.Rows[0];
                    string title = Pes.Utility.Conversion.TryCastString(row["Title"]);
                    string category = Pes.Utility.Conversion.TryCastString(row["CategoryName"]);
                    int categoryId = Pes.Utility.Conversion.TryCastInteger(row["CategoryId"]);
                    string addedOn = Pes.Utility.Conversion.TryCastString(row["AddedOn"]);
                    string seller = Pes.Utility.Conversion.TryCastString(row["DisplayName"]);
                    string totalViews = Pes.Utility.Conversion.TryCastString(row["TotalViews"]);
                    decimal price = Pes.Utility.Conversion.TryCastDecimal(row["Price"]);
                    string details = Pes.Utility.Conversion.TryCastString(row["Details"]);
                    string salt = Pes.Utility.Conversion.TryCastString(row["Salt"]);

                    string url = MixNP.BusinessLayer.Items.GetItemUrl(itemId, this.Page);

                    card.Attributes.Add("OnClick", "window.location='" + url + "'");
                    ImageLiteral.Text = "<img alt='" + title + "' src='/Handlers/GetItemImage.ashx?W=150&H=150&Path=" + MixNP.BusinessLayer.Items.GetRandomImage(itemId) + "' />";
                    TitleLiteral.Text = title;

                    if (price > 0)
                    {
                        TitleLiteral.Text += " for Rs. " + price.ToString("G29");
                    }

                    TitleLiteral.Text = "<a href='" + url + "' title='" + TitleLiteral.Text + "'>" + TitleLiteral.Text + "</a>";

                    CategoryLiteral.Text = "<a href='" + MixNP.BusinessLayer.Categories.GetCategoryUrl(categoryId, this.Page) + "' title='All Ads under " + category + "'>" + category + "</a>";
                    SellerNameLiteral.Text = "<a href='/user/" + salt + ".mix' title='All Ads by " + seller + "'>" + seller + "</a>";
                    DetailsLiteral.Text = TruncateWithPreservation(details.Replace(Environment.NewLine, " "), 320) + "...";
                }
            }
        }


        public string TruncateWithPreservation(string s, int len)
        {

            var builder = new StringBuilder(s.Length);

            foreach (char i in s)
                if (i != '\n' && i != '\r' && i != '\t')
                    builder.Append(i);

            s = builder.ToString();


            string[] parts = s.Split(' ');
            StringBuilder sb = new StringBuilder();

            foreach (string part in parts)
            {
                if (sb.Length + part.Length > len)
                    break;

                sb.Append(' ');
                sb.Append(part);
            }

            return sb.ToString();
        }
    }
}